//
//  CityTableViewController.m
//  Weather
//
//  Created by lanou3g on 15/7/20.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "CityTableViewController.h"
#import "City.h"
#import "RequestData.h"




@interface CityTableViewController ()<RequestFinish>

@property (nonatomic,retain)NSMutableArray *keyArray;//ABCD
@property (nonatomic,retain)NSMutableArray *citysArray;//城市的名字
@property (nonatomic,retain)NSMutableArray *cityArray;//每个模块中的字典
@property (nonatomic,retain)NSMutableArray *weatheridArray;
@property (nonatomic,retain)NSString *typeName;//区标题名字
@property (nonatomic,retain)NSMutableArray *array;
@property (nonatomic,retain)NSMutableArray *DataArray;

@end

@implementation CityTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    //    全国城市的具体信息
    //http://api.cmstop.net/mobile/index.php?app=mobile&controller=weather&action=index
    

    
    

    RequestData *request = [[RequestData alloc] init];
    [request requestWithUrl:@"http://api.cmstop.net/mobile/index.php?app=mobile&controller=weather&action=index"];
    request.delegate = self;
    self.cityArray = [NSMutableArray array];
    self.keyArray = [NSMutableArray array];

  
    
}


-(void)requestSuccess:(NSData *)data {
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

    self.array = [NSMutableArray array];

    self.DataArray = dic[@"data"];
    
    for (NSDictionary *dic1 in self.DataArray) {
        NSArray *array2 = dic1[@"citys"];
        NSString *str = [NSString string];
        str = dic1[@"name"];
        [self.keyArray addObject:str];
        
        NSMutableArray *array3 = [NSMutableArray array];
        
        for (NSDictionary *dic2 in array2) {
            City *city = [City new];
            city.name = dic2[@"name"];
            city.weatherid = dic2[@"weatherid"];
            [array3 addObject:city];
            self.array = array3;
            
            [self.weatheridArray addObject:city.weatherid];
            [self.citysArray addObject:city.name];
            
        }
        [self.cityArray addObject:self.array];
        [self.tableView reloadData];
    }
    
    
    
    
    
    
    
}

-(void)requestError:(NSError *)error {
    
    
    
    
}








//分区

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  
    [self.keyArray sortedArrayUsingSelector:@selector(compare:)];
    return [self.keyArray count];
}

//设置每个区的名字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    return self.keyArray[section];
}

//每个区的行数  ????                必须实现的两个方法之一
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    
//    NSArray *countArray = [self.array objectForKey:self.keyArray[section]];

    NSArray *array = self.cityArray[section];
    
    return array.count;
}
//创建索引标题
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.keyArray;
}



//创建cell 将数据放到cell上      也是必须实现的一个方法
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //设置cell的重用标识
    static NSString *identifier = @"cell";
    //cell的重用 有可以重用的就直接用 没有就创建
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    
//    NSArray *allArray = [self.cityDic objectForKey:self.keyArray[indexPath.section]];
    NSArray *array = self.cityArray[indexPath.section];
    City *city = array[indexPath.row];
    cell.textLabel.text = city.name;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    UIView *selectionView = [[UIView alloc] initWithFrame:cell.bounds];
    selectionView.backgroundColor = [UIColor cyanColor];
    //设置选中的cell的颜色 自定义
    cell.selectedBackgroundView = selectionView;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    

    
    
    NSString *a = [[[[[self.DataArray objectAtIndex:indexPath.section]objectForKey:@"citys"] objectAtIndex:indexPath.row] objectForKey:@"name"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *b = [[[[self.DataArray objectAtIndex:indexPath.section]objectForKey:@"citys"] objectAtIndex:indexPath.row] objectForKey:@"weatherid"];
    
    
    
    self.myBlock(a,b);
    
//    [self.navigationController popToRootViewControllerAnimated:YES];
//    [self.navigationController popToViewController:weatherVC animated:YES];
    [self dismissViewControllerAnimated:YES completion:nil ];
    
    
}



/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
